package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;

import controller.DataBaseInteactor;

import model.Author;
import model.Conference;

/**
 * The view of the user when he/she login as Author role.
 * 
 * @author Richard Lee
 * @version 52813
 */
public class AuthorView extends JFrame {
	
	/**
	 * Default serial version id.
	 */
	private static final long serialVersionUID = -3902087697570394571L;
	
	/**
	 * Conference that user belongs to.
	 */
	private Conference conference;
	
	/**
	 * GUI components.
	 */
	private final Font titleFont = new Font("Verdana", Font.BOLD, 30);
	private final JLabel titleLabel = new JLabel();
	private final JLabel dateLabel = new JLabel();
	private final JPanel topPanel = new JPanel(new BorderLayout());
	private final JPanel buttonsPanel = new JPanel(new FlowLayout());
	private final JButton viewButton = new JButton("View");
	private final JButton sumitManButton = new JButton("Sumit Manuscript");
	private final JButton unSumitManButton = new JButton("Unsumit Manuscript");
	private final JButton logoutButton = new JButton("Log out");
	
	/**
	 * Id of the user.
	 */
	private final String id;
	
	/**
	 * The manuscript table.
	 */
	private  JTable manTabel;
	
	/**
	 * Test contents in table.
	 */
	private Object[][] manuscriptData = {{"Title1", "Nick", "NOT REVIEWED",  "YES", "NO"},
			{"Title2", "Yang", "ACCEPTED", "YES", "YES"}};
	
	/**
	 * Database interactor.
	 */
	private final DataBaseInteactor inteactor;
	
	/**
	 * Constructor. 
	 * 
	 * @param conf the conference user belongs to.
	 * @param theInteactor database interactor.
	 * @param theId user id.
	 */
	public AuthorView(Conference conf, DataBaseInteactor theInteactor, String theId) {
		super("Author");
		conference = conf;
		inteactor = theInteactor;
		setLayout(new BorderLayout());
		id = theId;
	}
	
	/**
	 * Constructor.
	 * 
	 * @param theInteactor database interactor.
	 * @param theId user id.
	 */
	public AuthorView(DataBaseInteactor theInteactor, String theId) {
		super("Author");
		inteactor = theInteactor;
		id = theId;
		setLayout(new BorderLayout());
	}
	
	/**
	 * Combine and add all the GUI components.
	 */
	public void createAuthorView() {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		StringBuilder sb = new StringBuilder();
		sb.append("Manuscript Due Date: ");
		sb.append(conference.getManDate());
		sb.append(" Review Due Date: ");
		sb.append(conference.getreviewDate());
		sb.append(" Recommendation Due Date: ");
		sb.append(conference.getRecomDate());
		dateLabel.setText(sb.toString());
		titleLabel.setText(conference.getName());
		titleLabel.setFont(titleFont);
		topPanel.add(titleLabel, BorderLayout.PAGE_START);
		topPanel.add(dateLabel, BorderLayout.CENTER);
		p.add(topPanel);
		
		/**
		 * Table headers.
		 */
		String[] columnNames = {"Title", "Author", "Stauts", "Review", "Recomendation"};
		
		manTabel = new JTable(manuscriptData, columnNames);
		buttonsPanel.add(viewButton);
		buttonsPanel.add(sumitManButton);
		buttonsPanel.add(unSumitManButton);
		buttonsPanel.add(logoutButton);
		p.add(manTabel.getTableHeader());
		p.add(manTabel);
		p.add(buttonsPanel);
		add(p);
		pack();
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		actionButton();
	}
	
	/**
	 * Add actions to button.
	 */
	private void actionButton() {
		sumitManButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManuscriptSumitView msv = new ManuscriptSumitView(inteactor, id, conference);
				msv.createManuscriptSumitView();
			}
		});
		
		logoutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				LogInFrame loginframe = new LogInFrame(inteactor);
				loginframe.setVisible(true);
			}
		});
		
		unSumitManButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String manTitle = (String) manuscriptData[x][0];
				Author author = new Author(id, inteactor);
				author.removeManuscript(id, conference, manTitle);
			}
		});
		
		viewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				ViewView view = new ViewView(author, manTitle, conference, inteactor);
				view.createViewView();
			}
		});
	}
	
	
}
